home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / GEOFACE / make_face.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  13.9 KB  |  530 lines

  1. /* ==========================================================================
  2.                           MAKE_FACE_C
  3. =============================================================================
  4.  
  5.     FUNCTION NAMES
  6.  
  7.     face_reset            -- reset the face to neutral.
  8.     create_face                 -- creates the face data
  9.     create_face            -- returns a pointer to the head datastructure.
  10.     make_face            -- makes a face from the two input files.
  11.     add_polygon_to_face        -- adds a polygon to the face data structure.
  12.     reflect_polygon         -- reflect a polygon in the Y axis.
  13.     void averaged_vertex_normals - compute the average vertex normals.
  14.     data_struct         -- create the datastructure for the face.
  15.  
  16.     C SPECIFICATIONS
  17.  
  18.     void face_reset     ( HEAD *face )
  19.     HEAD *create_face     ( char *f1, char *f2 )    
  20.     HEAD *create_face     ( f1, f2 )
  21.     make_face         ( HEAD *face ) 
  22.     add_polygon_to_face ( POLYGON *p, HEAD *face )
  23.     reflect_polygon     ( POLYGON *poly, HEAD *face ) 
  24.     void averaged_vertex_normals ( HEAD *face, int p, 
  25.                                    float *n1, float *n2, float *n3 ) 
  26.     data_struct     ( HEAD *face )
  27.  
  28.     DESCRIPTION
  29.  
  30.     This module is where the face data structures are created.
  31.     T his module comes as is with no warranties.  
  32.  
  33.     HISTORY
  34.     16-Dec-94  Keith Waters (waters) at DEC's Cambridge Research Lab
  35.     Created.
  36.     Modified 22-Nov-96 Sing Bing Kang (sbk@crl.dec.com)
  37.       Added function expressions() to enable changing facial expression
  38.  
  39. ============================================================================ */
  40.  
  41. #include <math.h>           /* C header for any math functions               */
  42. #include <stdio.h>          /* C header for standard I/O                     */
  43. #include <string.h>         /* For String compare                            */
  44. #include <stdlib.h>
  45. #ifndef _WIN32
  46. #include <sys/types.h>
  47. #include <sys/file.h>
  48. #endif
  49.  
  50. #include "memory.h"         /* Local memory allocation macros                */
  51. #include "head.h"        /* local header for the face             */
  52.  
  53. void reflect_polygon ( POLYGON *poly, HEAD *face );
  54. void add_polygon_to_face ( POLYGON *p, HEAD *face );
  55. void make_face ( HEAD *face );
  56.  
  57. /* ========================================================================= */  
  58. /* face_reset                                                                 */
  59. /* ========================================================================= */  
  60. /*
  61. ** Resets the geometry of the face to neutral.
  62. **
  63. */
  64.  
  65. void face_reset ( HEAD *face )
  66. {
  67.   int i,j,k ;
  68.  
  69.   
  70.   for ( i=0; i<face->npolygons; i++ ) {
  71.  
  72.     for ( j=0; j<3; j++ ) {
  73.  
  74.       for ( k=0; k<3; k++ ) {
  75.     face->polygon[i]->vertex[j]->xyz[k] = 
  76.       face->polygon[i]->vertex[j]->nxyz[k] ;
  77.       }
  78.     }
  79.   }
  80. }
  81.  
  82.  
  83. /* ========================================================================= 
  84.    expressions                                                            
  85.    Written by: Sing Bing Kang
  86.    Date: 11/22/96
  87.    ========================================================================= */  
  88. /*
  89. ** Produces the facial expressions as indicated by the muscle contraction
  90. ** vector
  91. **
  92. */
  93.  
  94. void
  95. expressions ( HEAD *face, int e )
  96. {
  97.     int m;
  98.  
  99.     fprintf( stderr, "Expression: %s\n", face->expression[e]->name );
  100.  
  101.     for (m=0; m<face->nmuscles; m++) {
  102.     float m_val = face->expression[e]->m[m],
  103.               m_diff = m_val - face->muscle[m]->mstat;
  104.  
  105.     face->muscle[m]->mstat = m_val;
  106.         activate_muscle ( face,
  107.                          face->muscle[m]->head,
  108.                          face->muscle[m]->tail,
  109.                          face->muscle[m]->fs,
  110.                          face->muscle[m]->fe,
  111.                          face->muscle[m]->zone,
  112.                          m_diff ) ;
  113.     }
  114.  
  115. }
  116.  
  117. /* ========================================================================= */  
  118. /* create_face                                                                 */
  119. /* ========================================================================= */  
  120. /*
  121. ** create the default structures for the face and retrun a pointer.
  122. **
  123. */
  124.  
  125. HEAD *create_face ( char *f1, char *f2 )
  126. {
  127.   HEAD *h ;
  128.   
  129.   h = _new ( HEAD ) ;
  130.   
  131.   h->npolygons        = 0 ;
  132.   h->npindices        = 0 ;
  133.   h->npolylinenodes    = 0 ;
  134.   h->nmuscles        = 0 ;
  135.  
  136.   read_polygon_indices ( f1, h ) ;
  137.   read_polygon_line    ( f2, h ) ;
  138.  
  139.   make_face ( h ) ;
  140.   
  141.   return ( h ) ;
  142.  
  143. }
  144.  
  145. /* ========================================================================= */  
  146. /* make_face                                                                 */
  147. /* ========================================================================= */  
  148. /*
  149. ** makes the face from the two input files.
  150. **
  151. */
  152.  
  153. void
  154. make_face ( HEAD *face ) 
  155. {
  156.   POLYGON  *p ;
  157.   int i, ii, j, k,
  158.       p1, p2, p3, p4 ;
  159.   int parray[4] ;
  160.  
  161.   for ( i=0, ii=0; i < face->npindices; i++,ii+=4 ) {
  162.  
  163.     p1 = face->indexlist[ii]   -1 ;
  164.     p2 = face->indexlist[ii+1] -1 ;
  165.     p3 = face->indexlist[ii+2] -1 ;
  166.     p4 = face->indexlist[ii+3] -1 ;
  167.  
  168.     for (j=0; j<4; j++)
  169.       parray[j] = face->indexlist[ii+j] -1;
  170.  
  171.     if ( p1 == 999 ) {
  172.  
  173.       p = _new ( POLYGON ) ;
  174.       for (j=0; j<3; j++) {
  175.     p->vertex[j] = _new ( VERTEX ) ;
  176.     p->vertex[j]->np = 0 ;
  177.       }
  178.  
  179.       for (j=0; j<3; j++) 
  180.     p->vertex[0]->nxyz[j] = 
  181.     p->vertex[0]->xyz[j]  = face->polyline[ p2*3 + j ] ;
  182.       
  183.       for (j=0; j<3; j++) 
  184.     p->vertex[1]->nxyz[j] = 
  185.         p->vertex[1]->xyz[j]  = face->polyline[ p3*3 + j ] ;
  186.       
  187.       for (j=0; j<3; j++) 
  188.     p->vertex[2]->nxyz[j] = 
  189.         p->vertex[2]->xyz[j]  = face->polyline[ p4*3 + j ] ;
  190.  
  191.       add_polygon_to_face ( p, face ) ;
  192.       reflect_polygon     ( p, face ) ;
  193.     }
  194.     else {
  195.       p = _new ( POLYGON ) ;
  196.       for (j=0; j<3; j++) {
  197.     p->vertex[j] = _new ( VERTEX ) ;
  198.     p->vertex[j]->np = 0 ;
  199.       }
  200.  
  201.       for (k=0; k<3; k++) {
  202.     for (j=0; j<3; j++) 
  203.       p->vertex[k]->nxyz[j] =
  204.       p->vertex[k]->xyz[j]  = face->polyline[ parray[k]*3 + j ] ;
  205.       }
  206.  
  207.       add_polygon_to_face ( p, face ) ;
  208.       reflect_polygon     ( p, face ) ;
  209.  
  210.       p = _new ( POLYGON ) ;
  211.       for (j=0; j<3; j++) {
  212.     p->vertex[j] = _new ( VERTEX ) ;
  213.     p->vertex[j]->np = 0 ;
  214.       }
  215.  
  216.       for (j=0; j<3; j++) 
  217.     p->vertex[0]->nxyz[j] = 
  218.     p->vertex[0]->xyz[j]  = face->polyline[ p1*3 + j ] ;
  219.       
  220.       for (j=0; j<3; j++) 
  221.     p->vertex[1]->nxyz[j] =
  222.     p->vertex[1]->xyz[j]  = face->polyline[ p3*3 + j ] ;
  223.       
  224.       for (j=0; j<3; j++) 
  225.     p->vertex[2]->nxyz[j] = 
  226.     p->vertex[2]->xyz[j]  = face->polyline[ p4*3 + j ] ;
  227.  
  228.       add_polygon_to_face ( p, face ) ;
  229.       reflect_polygon     ( p, face ) ;
  230.       }
  231.   }
  232.  
  233. }
  234.  
  235. /* ========================================================================= */  
  236. /* add_polygon_to_face                                                       */
  237. /* ========================================================================= */  
  238. /*
  239. ** add a polygon to the face structure.
  240. **
  241. */
  242.  
  243. void
  244. add_polygon_to_face ( POLYGON *p, HEAD *face )
  245. {
  246.   int nn ;
  247.  
  248.   if(face->npolygons == 0)
  249.       face->polygon = _new_array(POLYGON *, 500) ;
  250.   else if(face->npolygons % 500 == 0)
  251.       face->polygon = _resize_array(face->polygon,POLYGON *,face->npolygons+500) ;
  252.  
  253.   nn = face->npolygons ;
  254.   face->polygon[nn] = p ;
  255.  
  256.   face->npolygons++ ;
  257.  
  258. }
  259.  
  260.  
  261. /* ========================================================================= */  
  262. /* reflect_polygon                                                           */
  263. /* ========================================================================= */  
  264. /*
  265. **  Reflects all the polygons in the half-face and adds them to
  266. **  the data structure.
  267. **
  268. */
  269.  
  270. void
  271. reflect_polygon ( POLYGON *poly, HEAD *face ) 
  272. {
  273.   POLYGON *newp ;
  274.   float   temp[3] ;
  275.   int      i, j ;
  276.   
  277.   /* 
  278.    * Allocate memory for the new polygon. 
  279.   */
  280.   newp = _new ( POLYGON ) ;
  281.   for (j=0; j<3; j++) {
  282.     newp->vertex[j] = _new ( VERTEX ) ;
  283.     newp->vertex[j]->np = 0 ;
  284.   }
  285.  
  286.   /* 
  287.    * Load the old polygon values. 
  288.   */
  289.   for (i=0; i<3; i++) 
  290.     for (j=0; j<3; j++)
  291.       newp->vertex[i]->nxyz[j] = 
  292.       newp->vertex[i]->xyz[j]  = poly->vertex[i]->xyz[j] ;
  293.  
  294.   /* 
  295.    * flip the X component.         
  296.   */
  297.   for (i=0; i<3; i++) 
  298.     newp->vertex[i]->nxyz[0] = 
  299.     newp->vertex[i]->xyz[0]  = -newp->vertex[i]->xyz[0] ;
  300.   
  301.   /* 
  302.    * Re-order the vertices, flip 0 and 1.
  303.   */
  304.   for (j=0; j<3; j++)
  305.     temp[j] = newp->vertex[0]->xyz[j] ;
  306.  
  307.   for (j=0; j<3; j++)
  308.     newp->vertex[0]->nxyz[j] = 
  309.     newp->vertex[0]->xyz[j]  = newp->vertex[1]->xyz[j];
  310.  
  311.   for (j=0; j<3; j++)
  312.     newp->vertex[1]->nxyz[j] = 
  313.     newp->vertex[1]->xyz[j]  = temp[j] ;
  314.  
  315.   add_polygon_to_face ( newp, face ) ;
  316.  
  317. }
  318.  
  319. /* ========================================================================= */  
  320. /* averaged_vertex_normals                                                   */
  321. /* ========================================================================= */  
  322. /*
  323. ** Caculates the averaged polygon normal.
  324. */
  325.  
  326. void averaged_vertex_normals ( HEAD *face, int p, float *n1, float *n2, float *n3 ) 
  327. {
  328.   int i,j,np, pt ;
  329.   float norm[3] ;
  330.  
  331.  
  332.   for (i=0; i<3; i++)
  333.     norm[i] = 0.0 ;
  334.  
  335.   np = face->polygon[p]->vertex[0]->np ;
  336.  
  337.   for ( i=0; i<np; i++) {
  338.     pt = face->polygon[p]->vertex[0]->plist[i] ;
  339.  
  340.     for ( j=0; j<3; j++)  {
  341.       norm[j] += face->polygon[pt]->vertex[0]->norm[j] ;
  342.     }
  343.   }
  344.  
  345.   for (i=0; i<3; i++)
  346.     norm[i] = norm[i] / (float)np ;
  347.     
  348.   for (i=0; i<3; i++)
  349.     n1[i] = norm[i] ;
  350.  
  351.   for (i=0; i<3; i++)
  352.     norm[i] = 0.0 ;
  353.  
  354.   np = face->polygon[p]->vertex[1]->np ;
  355.  
  356.   for ( i=0; i<np; i++) {
  357.     pt = face->polygon[p]->vertex[1]->plist[i] ;
  358.  
  359.     for ( j=0; j<3; j++) {
  360.       norm[j] += face->polygon[pt]->vertex[1]->norm[j] ;
  361.     }
  362.   }
  363.  
  364.   for (i=0; i<3; i++)
  365.     norm[i] = norm[i] / (float) np ;
  366.  
  367.   for (i=0; i<3; i++)
  368.     n2[i] = norm[i] ;
  369.  
  370.   for (i=0; i<3; i++)
  371.     norm[i] = 0.0 ;
  372.  
  373.   np = face->polygon[p]->vertex[2]->np ;
  374.  
  375.   for ( i=0; i<np; i++) {
  376.     pt = face->polygon[p]->vertex[2]->plist[i] ;
  377.  
  378.     for ( j=0; j<3; j++) {
  379.       norm[j] += face->polygon[pt]->vertex[2]->norm[j] ;
  380.     }
  381.   }
  382.  
  383.   for (i=0; i<3; i++)
  384.     norm[i] = norm[i]/ (float) np ;
  385.     
  386.   for (i=0; i<3; i++)
  387.     n3[i] = norm[i] ;
  388.    
  389. }
  390.  
  391. /* ========================================================================= */  
  392. /* data_struct                                                                 */
  393. /* ========================================================================= */  
  394. /*
  395. ** Create a new data structure for the polygons.
  396. **
  397. */
  398. #define DATA_STRUCT_DEBUG 0
  399.  
  400. void
  401. data_struct ( HEAD *face )
  402. {
  403.   int i,j, n ;
  404.   int flag, cptr ;
  405.   float x1,y1,z1, x2, y2, z2, x3, y3, z3 ;
  406.   float tx1, ty1, tz1, tx2, ty2, tz2, tx3, ty3, tz3 ;
  407.  
  408.   for (i=0; i<face->npolygons; i++ ){
  409.  
  410.       x1 = face->polygon[i]->vertex[0]->xyz[0] ;
  411.       y1 = face->polygon[i]->vertex[0]->xyz[1] ;
  412.       z1 = face->polygon[i]->vertex[0]->xyz[2] ;
  413.  
  414.       x2 = face->polygon[i]->vertex[1]->xyz[0] ;
  415.       y2 = face->polygon[i]->vertex[1]->xyz[1] ;
  416.       z2 = face->polygon[i]->vertex[1]->xyz[2] ;
  417.  
  418.       x3 = face->polygon[i]->vertex[2]->xyz[0] ;
  419.       y3 = face->polygon[i]->vertex[2]->xyz[1] ;
  420.       z3 = face->polygon[i]->vertex[2]->xyz[2] ;
  421. #if DATA_STRUCT_DEBUG
  422.       fprintf (stderr,"BASE polygon: %d\n", i) ;
  423.       fprintf (stderr,"x1: %f y1: %f z1: %f\n", x1,y1,z1) ;
  424.       fprintf (stderr,"x1: %f y1: %f z1: %f\n", x2,y2,z2) ;
  425.       fprintf (stderr,"x1: %f y1: %f z1: %f\n", x3,y3,z3) ;
  426. #endif
  427.       j    = 0 ;
  428.       flag = 0 ;
  429.       while ( !flag  &&
  430.           j<face->npolygons ) {
  431.  
  432.     tx1 = face->polygon[j]->vertex[0]->xyz[0] ;
  433.     ty1 = face->polygon[j]->vertex[0]->xyz[1] ;
  434.     tz1 = face->polygon[j]->vertex[0]->xyz[2] ;
  435.  
  436.     tx2 = face->polygon[j]->vertex[1]->xyz[0] ;
  437.     ty2 = face->polygon[j]->vertex[1]->xyz[1] ;
  438.     tz2 = face->polygon[j]->vertex[1]->xyz[2] ;
  439.  
  440.     tx3 = face->polygon[j]->vertex[2]->xyz[0] ;
  441.     ty3 = face->polygon[j]->vertex[2]->xyz[1] ;
  442.     tz3 = face->polygon[j]->vertex[2]->xyz[2] ;
  443. #if DATA_STRUCT_DEBUG
  444.     fprintf (stderr, "COMPARED TO polygon: %d\n", j) ;
  445.     fprintf (stderr,"tx1: %f ty1: %f tz1: %f\n", tx1,ty1,tz1) ;
  446.     fprintf (stderr,"tx1: %f ty1: %f tz1: %f\n", tx2,ty2,tz2) ;
  447.     fprintf (stderr,"tx1: %f ty1: %f tz1: %f\n", tx3,ty3,tz3) ;
  448. #endif    
  449.     if ( (x1 == tx1 && y1 == ty1 && z1 == tz1) ||
  450.          (x1 == tx2 && y1 == ty2 && z1 == tz2) ||
  451.          (x1 == tx3 && y1 == ty3 && z1 == tz3)) {
  452.       cptr = j ;
  453. #if DATA_STRUCT_DEBUG
  454.       fprintf (stderr,"found a vertex match on polygon: %d and %d\n", i,j);
  455. #endif
  456.       n = face->polygon[i]->vertex[0]->np ;
  457.       face->polygon[i]->vertex[0]->plist[n] = cptr ;
  458.       face->polygon[i]->vertex[0]->np++ ;
  459. #if DATA_STRUCT_DEBUG
  460.       fprintf (stderr,"loaded: %d onto polygon: %d vertex[0]\n", cptr, i) ;
  461.       fprintf (stderr,"total on vertex: %d\n", face->polygon[i]->vertex[0]->np);
  462. #endif
  463.     }
  464.     j++ ;
  465.  
  466.       } /* end while */
  467.  
  468.  
  469.       j    = 0 ;
  470.       flag = 0 ;
  471.       while ( !flag  &&
  472.           j<face->npolygons ) {
  473.     tx1 = face->polygon[j]->vertex[0]->xyz[0] ;
  474.     ty1 = face->polygon[j]->vertex[0]->xyz[1] ;
  475.     tz1 = face->polygon[j]->vertex[0]->xyz[2] ;
  476.  
  477.     tx2 = face->polygon[j]->vertex[1]->xyz[0] ;
  478.     ty2 = face->polygon[j]->vertex[1]->xyz[1] ;
  479.     tz2 = face->polygon[j]->vertex[1]->xyz[2] ;
  480.  
  481.     tx3 = face->polygon[j]->vertex[2]->xyz[0] ;
  482.     ty3 = face->polygon[j]->vertex[2]->xyz[1] ;
  483.     tz3 = face->polygon[j]->vertex[2]->xyz[2] ;
  484.  
  485.     if ( (x2 == tx1 && y2 == ty1 && z2 == tz1) ||
  486.          (x2 == tx2 && y2 == ty2 && z2 == tz2) ||
  487.          (x2 == tx3 && y2 == ty3 && z2 == tz3)) {
  488.       cptr = j ;
  489.  
  490.       n = face->polygon[i]->vertex[1]->np ;
  491.       face->polygon[i]->vertex[1]->plist[n] = j ;
  492.       face->polygon[i]->vertex[1]->np++ ;
  493.  
  494.     }
  495.     j++ ;
  496.  
  497.       } /* end while */
  498.  
  499.       j    = 0 ;
  500.       flag = 0 ;
  501.       while ( !flag  &&
  502.           j<face->npolygons ) {
  503.     tx1 = face->polygon[j]->vertex[0]->xyz[0] ;
  504.     ty1 = face->polygon[j]->vertex[0]->xyz[1] ;
  505.     tz1 = face->polygon[j]->vertex[0]->xyz[2] ;
  506.  
  507.     tx2 = face->polygon[j]->vertex[1]->xyz[0] ;
  508.     ty2 = face->polygon[j]->vertex[1]->xyz[1] ;
  509.     tz2 = face->polygon[j]->vertex[1]->xyz[2] ;
  510.  
  511.     tx3 = face->polygon[j]->vertex[2]->xyz[0] ;
  512.     ty3 = face->polygon[j]->vertex[2]->xyz[1] ;
  513.     tz3 = face->polygon[j]->vertex[2]->xyz[2] ;
  514.  
  515.     if ( x3 == tx1 &&  y3 == ty1 &&  z3 == tz1 ||
  516.          x3 == tx2 &&  y3 == ty2 &&  z3 == tz2 ||
  517.          x3 == tx3 &&  y3 == ty3 &&  z3 == tz3) {
  518.       cptr = j ;
  519.  
  520.       n = face->polygon[i]->vertex[2]->np ;
  521.       face->polygon[i]->vertex[2]->plist[n] = cptr ;
  522.       face->polygon[i]->vertex[2]->np++ ;
  523.  
  524.     }
  525.     j++ ;
  526.  
  527.       } /* end while */
  528.     } /* end for i */
  529. }
  530.